$(function() {
	
	checkboxIfSelectAll();
	//ajaxCartItemShow();
})



function ajaxCartItemShow(){
	$.ajax({
		type : "post",
		async : true,
		url : path+ "/manage/commodityCart/cartItemShow?marketId="+marketId,
		success : function(resultjson) {
			
		}
	})
}
 


// 修改购物车 sign 1 减 2 加
function saveOrUpdate(goodsId, sign, obj) {
	
	var amount = $("#goods_" + goodsId).val();
	if (sign == 1) {
		total = total - $("#price_" + goodsId).html();
		if (amount <= 1) {
			deleteGoods(goodsId,marketId);
			return;
		}
		$("#goods_" + goodsId).val(Number(amount) - 1);
	} else {
		total = total + $("#price_" + goodsId).html();
		$("#goods_" + goodsId).val(Number(amount) + 1);

	}

	updateCart(goodsId, $("#goods_" + goodsId).val(),marketId)
	
}
 

//删除购物车的商品
function deleteGoods(goodsId,marketId){
	common.alert({
		dialog : true,
		content : '确认删除该商品吗',
		ok : function() { 
			$.ajax({
				type : "post",
				async : false,
				url : path
						+ "/manage/commodityCart/delCartItem?goodsId="
						+ goodsId + "&marketId=" + marketId,
				success : function(resultjson) {
					if ('000' == resultjson.code) {
						// $(obj).parents(".num-module").parents(".goods").siblings(".subtotal").find("span").html("小计：￥"+resultjson.result.MerchantPrice+"");
						// $("#total").html("合计：￥"+resultjson.result.cartPrice+"元");
						 window.location.reload();
					}
				}
			})
			
		},
		cancelValue : '取消',
		cancel : function() {
		}
	})

}

//输入框修改
//function  updateInput(){
//	$("")
//}




function updateCart(goodsId,amount,marketId){
	// 修改购物车异步请求
	$.ajax({
		type : "post",
		async : false,
		url : path + "/manage/commodityCart/update?goodsId=" + goodsId
				+ "&amount=" + $("#goods_" + goodsId).val() + "&marketId="
				+ marketId,
		success : function(resultjson) {
			if ('000' == resultjson.code) {
				// $(obj).parents(".num-module").parents(".goods").siblings(".subtotal").find("span").html("小计：￥"+resultjson.result.MerchantPrice+"");
				$("#total").html("合计：￥" + resultjson.result.cartPrice + "元");
			}
		}
	})
}










// 监控所有商品是否全选
function checkboxIfSelectAll() {
	var ifSelectAll = true;
	$("input[name='checkboxname']:checkbox").each(function() {

		if ($(this).attr("checked")) {
			// alert(this.value); //value是checkbox的值
		} else {
			ifSelectAll = false;
		}
	});
	if (ifSelectAll) {
		$("#selectAll").prop('checked', true);
	} else {
		$("#selectAll").prop('checked', false);
	}
}



// 选中购物车的商品
function checkCartItem(check, goodsId, obj) {

	var allcheck = $("input[name='checkboxname']:checkbox").length;// checkbox数量
	var checked = $("input[name='checkboxname']:checked").length;// 选中的数量

	if (allcheck == checked) {
		total = total + $("#price_" + goodsId).html();
		$("#total").html("合计：￥" + parseFloat(total).toFixed(2) + "元");
		$("#selectAll").prop('checked', true);
	} else {
		total = total - $("#price_" + goodsId).html();
		$("#total").html("合计：￥" + parseFloat(total).toFixed(2) + "元");
		$("#selectAll").prop('checked', false);
	}

	var amount = $("#goods_" + goodsId).val();
	$.ajax({
		type : "post",
		async : false,
		url : path + "/manage/commodityCart/checkCartItem?goodsId=" + goodsId
				+ "&status=" + check + "&marketId=" + marketId + "&amount="
				+ amount,
		success : function(resultjson) {
			if ('000' == resultjson.code) {
				// 单独商品选中状态 1未选中
				if (check == 1) {
					$("#cart_item_" + goodsId + " .checkbox").attr("onclick",
							"checkCartItem(2," + goodsId + ",this)");
				} else {
					$("#cart_item_" + goodsId + " .checkbox").attr("onclick",
							"checkCartItem(1," + goodsId + ",this)");
				}
				$("#cart_item_" + goodsId + " .checkbox").attr("check", check);

//				$(obj).parents(".shop").find(".subtotal span").html(
//						"小计：￥" + resultjson.result.MerchantPrice + "");
				$("#total").html("合计：￥" + resultjson.result.cartPrice + "元");

			}
		}
	})
}

//选中购物车的异步请求
function checkCart(check,obj) {
	$.ajax({
		type : "post",
		async : false,
		url : path + "/manage/commodityCart/checkCart?status=" + check
				+ "&marketId=" + marketId,
		success : function(resultjson) {
			if ('000' == resultjson.code) {
				$("input[type='checkbox']").prop('checked', $(obj).prop('checked'));
				//选中
				if (obj.prop('checked')) {
					total = 0.0;
					//遍历计算总金额
					$("input[name='checkboxname']:checkbox").each(function() {
						var goodsId = $(this).data("id");
						console.log("数量" + Number($("#goods_" + goodsId).val()));
						console.log("jaige " + $("#price_" + goodsId).html());
						var number = Number($("#goods_" + goodsId).val());
						var price = parseFloat($("#price_" + goodsId).html()).toFixed(2);
						total += number * price;
						$("#cart_item_" + goodsId + " .checkbox").attr("onclick",
								"checkCartItem(1," + goodsId + ",this)");
					});
					
					$("#total").html("合计：￥" + parseFloat(total).toFixed(2) + "元");
				} else {
					$("input[name='checkboxname']:checkbox").each(function() {
						var goodsId = $(this).data("id");
						$("#cart_item_" + goodsId + " .checkbox").attr("onclick",
								"checkCartItem(2," + goodsId + ",this)");
					});
					$("#total").html("合计：￥ 0.0元");
				}
			}
		}
	})
}

// 全选
$("#selectAll").on("click", function() {

	var obj = $(this);
	

	if (obj.prop('checked')) {
		
		checkCart(2,obj);
	} else {
		
		checkCart(1,obj);
	}

});
